<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use	Exception;
use App\User;
use App\Online;
use App\Http\Controllers\UsersController as Users;
use App\Http\Controllers\CheckController as Check;
use Illuminate\Database\Eloquent\Model;
use App\Account;
use App\Connect;
use App\Log_login;
use App\Log_logout;
use App\Product;
use App\Coupon;


class UserWebController extends Controller
{
    //登录
    public function login(Request $request)
    {

        $ip = '';
        $mac = '';
        if (!empty($request->input('ip'))){
            $ip = $request->input('ip');
        }
        if (!empty($request->input('mac'))){
            $mac = $request->input('mac');
        }
        //记录登陆日志
        $log_login = new Log_login();
        $log_login->password = '';
        $log_login->ip = $ip;
        $log_login->mac = $mac;
        $log_login->suc_flag = 0;

        if (empty($request->input('name'))){
            $log_login->save();
            $return = array(
                'code' => -1,
                'msg' => "用户名输入有误",
                'data' => array()
            );
            // 			$return = Utils::returnArray(-1,"用户名输入有误");
            return response()->json($return);
        }

        $name = $request->input('name');
        $log_login->user_name = $name;
        if (empty($request->input('password'))){
            $log_login->save();
            $return = array(
                'code' => -1,
                'msg' => "密码输入有误",
                'data' => array()
            );
            return response()->json($return);
        }

        $password = $request->input('password');
        $where = array(
            'name'=>$name,
            'password'=>$password
        );
        $user = User::where($where)->first();

        if(empty($user)){
            $log_login->password = $password;
            $log_login->save();
            $return = array(
                'code' => -1,
                'msg' => '用户名或密码错误',
                'data' => array()
            );
            return response()->json($return);
        }
        //生成token
        $token = md5($mac.$user->id.time());
        $user_brief = array(
            'id' => $user->id,
            'mobile' => $user->mobile,
            'email' => $user->email,
            'name' => $user->name,
            'qq' => $user->qq,
            'vip' => $user->vip,
            'vip_expired' => $user->vip_expired,
            'recommend' => $user->recommend,
            'remark' => $user->remark
        );

        $vip_flag = 0;
        //获取会员等级
        if (strtotime($user->vip_expired)>time()){
            $vip_flag = $user->vip;
        }

        $return = array(
            'code' => 0,
            'msg' => 'OK',
            'data' => array(
                'token'=>(string)$token,
                'vip_flag' => $vip_flag,
                'user' => $user_brief
            )
        );

        $log_login->user_id = $user->id;
        $log_login->suc_flag = 1;
        $log_login->token = $token;
        $log_login->terminal = 1;
        $log_login->save();
        return response()->json($return);
    }

    //注销
    public function logout(Request $request)
    {
        if (empty($request->input('token'))){
            $return = array(
                'code' => -1,
                'msg' => '用户信息有误',
                'data' => array()
            );
            return response()->json($return);
        }
        $token = $request->input('token');
        $id = Users::getIdByWebToken($token);

        //记录注销日志
        $log_logout = new Log_logout();
        $log_logout->user_id = $id;
        $log_logout->token = $token;

        //销毁token
        $token = "";

        $return = array(
            'code' => 0,
            'msg' => 'OK',
            'data' => array(
                'token'=>$token
            )
        );
        $log_logout->save();

        return response()->json($return);
    }


    public function getUserVipInfo(Request $request)
    {
        if (empty($request->input('token'))) {
            $return = array(
                'code' => -1,
                'msg' => '用户信息有误',
                'data' => array()
            );
            return response()->json($return);
        }
        $token = $request->input('token');
        $id = Users::getIdByWebToken($token);


        $user = User::find($id);

        $vip = 0;
        $vip_expired = '';
        //获取会员等级
        if (strtotime($user->vip_expired)>time()){
            $vip_expired = $user->vip_expired;
            $vip = $user->vip;
        }

        $return = array(
            'code' => 0,
            'msg' => 'OK',
            'data' => array(
                'token'=>(string)$token,
                'vip' => $vip,
                'vip_expired' => $vip_expired
            )
        );
        return response()->json($return);



    }



    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
     * 用户名获取用户ID
     */
    public function accountGetUserId( Request $request )
    {
        if (empty($request->input('account'))){
            $return = array(
                'code' => -1,
                'msg' => "缺少用户名",
                'data' => array()
            );
            // 			$return = Utils::returnArray(-1,"用户名输入有误");
            return response()->json($return);
        }

        $account =  $request->input('account');
        $user = Check::CheckAccount($account);

        if(!empty($user))
        {
            return $user->id;
        }else{
            $return = array(
                'code' => -1,
                'msg' => "用户不存在",
                'data' => array()
            );
            return response()->json($return);
        }


    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
     * 优惠券优惠
     */
    public function discountCoupon(Request $request )
    {
        if (empty($request->input('code'))){
            $return = array(
                'code' => -1,
                'msg' => "缺少优惠码",
                'data' => array()
            );
            return response()->json($return);
        }
        $code = $request->input('code');

        $coupon = Coupon::where('coupon_code',$code)->where('is_use','0')->first();

        if(!empty($coupon))
        {
            $return = array(
                'error_code' => 0,
                'error_message' => 'OK',
                'data' => array(
                    'coupon'=>$coupon
                )
            );

            return response()->json($return);
        }else{
            $return = array(
                'error_code' => -1,
                'error_message' => "非法优惠码",
                'data' => array()
            );
            return response()->json($return);
        }


    }

    /**
     * @param Request $request
     * @return bool|\Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
     * 检测用户是否存在
     */
    public function CheckAccount(Request $request)
    {
        if($request->has('account'))
        {
            $account = $request->input('account');
        }else{
            $return = array(
                'code' => -1,
                'msg' => "缺少用户名",
                'data' => array()
            );
            return response()->json($return);
        }

        $ret =  User::where('name',$account)->first();

       if(!empty($ret))
       {
           return true;
       }else{
           return true;
       }
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
     * 用户修改密码
     */
    public function updatePassword(Request $request)
    {
        if($request->has('token'))
        {
            $account = $request->input('token');
        }else{
            $return = array(
                'code' => -1,
                'msg' => "缺少token",
                'data' => array()
            );
            return response()->json($return);
        }
        $token = $request->input('token');
        $id = Users::getIdByWebToken($token);

        $user = User::find($id);


        if($request->has('password'))
        {
            $password = $request->input('password');
        }else{
            $return = array(
                'code' => -1,
                'msg' => "缺少password",
                'data' => array()
            );
            return response()->json($return);
        }

        if($request->has('password'))
        {
            $new_password = $request->input('new_password');
        }else{
            $return = array(
                'code' => -1,
                'msg' => "缺少new_password",
                'data' => array()
            );
            return response()->json($return);
        }

        if($user->password != $password)
        {
            $return = array(
                'code' => -1,
                'msg' => "密码错误",
                'data' => array()
            );
            return response()->json($return);
        }

        $user->password = $new_password;

        $user->save();

        $return = array(
            'error_code' => 0,
            'error_message' => ""
        );
        return response()->json($return);


    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
     * 更新qq
     */
    public function updateQQ(Request $request)
    {
        if($request->has('token'))
        {
            $account = $request->input('token');
        }else{
            $return = array(
                'code' => -1,
                'msg' => "缺少token",
                'data' => array()
            );
            return response()->json($return);
        }
        $token = $request->input('token');
        $id = Users::getIdByWebToken($token);

        $user = User::find($id);

        if($request->has('qq'))
        {
            $qq = $request->input('qq');
        }else{
            $return = array(
                'code' => -1,
                'msg' => "缺少qq",
                'data' => array()
            );
            return response()->json($return);
        }

        $user->qq = $qq;

        $user->save();

        $return = array(
            'error_code' => 0,
            'error_message' => ""
        );
        return response()->json($return);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
     * 修改邮箱
     */
    public function updateEmail(Request $request)
    {
        if($request->has('token'))
        {
            $account = $request->input('token');
        }else{
            $return = array(
                'code' => -1,
                'msg' => "缺少token",
                'data' => array()
            );
            return response()->json($return);
        }
        $token = $request->input('token');
        $id = Users::getIdByWebToken($token);

        $user = User::find($id);

        if($request->has('email'))
        {
            $email = $request->input('email');
        }else{
            $return = array(
                'code' => -1,
                'msg' => "缺少email",
                'data' => array()
            );
            return response()->json($return);
        }

        $user->email = $email;

        $user->save();


        $return = array(
            'error_code' => 0,
            'error_message' => ""
        );
        return response()->json($return);


    }


    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
     * 绑定手机号
     */
    public function bindPhone(Request $request)
    {
        if($request->has('token'))
        {
            $account = $request->input('token');
        }else{
            $return = array(
                'code' => -1,
                'msg' => "缺少token",
                'data' => array()
            );
            return response()->json($return);
        }
        $token = $request->input('token');
        $id = Users::getIdByWebToken($token);

        $user = User::find($id);

        if($request->has('phone'))
        {
            $phone = $request->input('phone');

            if(preg_match("/^[0-9]{11}$/",$phone)){
                $phone = $phone;
            }else{
                $data = array();
                $data['error_code'] = 2;
                $data['error_message'] = '手机号码不正确！';
                return response()->json($data);
            }
        }else{
            $return = array(
                'code' => -1,
                'msg' => "缺少phone",
                'data' => array()
            );
            return response()->json($return);
        }

        $user->mobile = $phone;

        $user->save();


        $return = array(
            'error_code' => 0,
            'error_message' => ""
        );
        return response()->json($return);


    }


}